﻿using System;
using System.Linq;
using System.Text;
using HslCommunication.BasicFramework;
using HslCommunication.Core;
using HslCommunication.Reflection;
using HslCommunication.Serial;

namespace HslCommunication.Profinet.Melsec
{
	/// <summary>
	/// 三菱计算机链接协议，适用FX3U系列，FX3G，FX3S等等系列，通常在PLC侧连接的是485的接线口<br />
	/// Mitsubishi Computer Link Protocol, suitable for FX3U series, FX3G, FX3S, etc., usually the 485 connection port is connected on the PLC side
	/// </summary>
	/// <remarks>
	/// <inheritdoc cref="T:HslCommunication.Profinet.Melsec.MelsecFxLinksOverTcp" path="remarks" />
	/// </remarks>
	// Token: 0x02000069 RID: 105
	public class MelsecFxLinks : SerialDeviceBase
	{
		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecFxLinksOverTcp.#ctor" />
		// Token: 0x0600087A RID: 2170 RVA: 0x0002DD1A File Offset: 0x0002BF1A
		public MelsecFxLinks()
		{
			base.ByteTransform = new RegularByteTransform();
			base.WordLength = 1;
		}

		/// <inheritdoc cref="P:HslCommunication.Profinet.Melsec.MelsecFxLinksOverTcp.Station" />
		// Token: 0x17000180 RID: 384
		// (get) Token: 0x0600087B RID: 2171 RVA: 0x0002DD4D File Offset: 0x0002BF4D
		// (set) Token: 0x0600087C RID: 2172 RVA: 0x0002DD55 File Offset: 0x0002BF55
		public byte Station
		{
			get
			{
				return this.station;
			}
			set
			{
				this.station = value;
			}
		}

		/// <inheritdoc cref="P:HslCommunication.Profinet.Melsec.MelsecFxLinksOverTcp.WaittingTime" />
		// Token: 0x17000181 RID: 385
		// (get) Token: 0x0600087D RID: 2173 RVA: 0x0002DD5E File Offset: 0x0002BF5E
		// (set) Token: 0x0600087E RID: 2174 RVA: 0x0002DD68 File Offset: 0x0002BF68
		public byte WaittingTime
		{
			get
			{
				return this.watiingTime;
			}
			set
			{
				bool flag = this.watiingTime > 15;
				if (flag)
				{
					this.watiingTime = 15;
				}
				else
				{
					this.watiingTime = value;
				}
			}
		}

		/// <inheritdoc cref="P:HslCommunication.Profinet.Melsec.MelsecFxLinksOverTcp.SumCheck" />
		// Token: 0x17000182 RID: 386
		// (get) Token: 0x0600087F RID: 2175 RVA: 0x0002DD99 File Offset: 0x0002BF99
		// (set) Token: 0x06000880 RID: 2176 RVA: 0x0002DDA1 File Offset: 0x0002BFA1
		public bool SumCheck
		{
			get
			{
				return this.sumCheck;
			}
			set
			{
				this.sumCheck = value;
			}
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecFxLinksOverTcp.Read(System.String,System.UInt16)" />
		// Token: 0x06000881 RID: 2177 RVA: 0x0002DDAC File Offset: 0x0002BFAC
		[HslMqttApi("ReadByteArray", "")]
		public override OperateResult<byte[]> Read(string address, ushort length)
		{
			byte b = (byte)HslHelper.ExtractParameter(ref address, "s", (int)this.station);
			OperateResult<byte[]> operateResult = MelsecFxLinksOverTcp.BuildReadCommand(b, address, length, false, this.sumCheck, this.watiingTime);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadBase(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = OperateResult.CreateFailedResult<byte[]>(operateResult2);
				}
				else
				{
					bool flag3 = operateResult2.Content[0] != 2;
					if (flag3)
					{
						result = new OperateResult<byte[]>((int)operateResult2.Content[0], "Read Faild:" + SoftBasic.ByteToHexString(operateResult2.Content, ' '));
					}
					else
					{
						byte[] array = new byte[(int)(length * 2)];
						for (int i = 0; i < array.Length / 2; i++)
						{
							ushort value = Convert.ToUInt16(Encoding.ASCII.GetString(operateResult2.Content, i * 4 + 5, 4), 16);
							BitConverter.GetBytes(value).CopyTo(array, i * 2);
						}
						result = OperateResult.CreateSuccessResult<byte[]>(array);
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecFxLinksOverTcp.Write(System.String,System.Byte[])" />
		// Token: 0x06000882 RID: 2178 RVA: 0x0002DEC8 File Offset: 0x0002C0C8
		[HslMqttApi("WriteByteArray", "")]
		public override OperateResult Write(string address, byte[] value)
		{
			byte b = (byte)HslHelper.ExtractParameter(ref address, "s", (int)this.station);
			OperateResult<byte[]> operateResult = MelsecFxLinksOverTcp.BuildWriteByteCommand(b, address, value, this.sumCheck, this.watiingTime);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadBase(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					bool flag3 = operateResult2.Content[0] != 6;
					if (flag3)
					{
						result = new OperateResult((int)operateResult2.Content[0], "Write Faild:" + SoftBasic.ByteToHexString(operateResult2.Content, ' '));
					}
					else
					{
						result = OperateResult.CreateSuccessResult();
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecFxLinksOverTcp.ReadBool(System.String,System.UInt16)" />
		// Token: 0x06000883 RID: 2179 RVA: 0x0002DF7C File Offset: 0x0002C17C
		[HslMqttApi("ReadBoolArray", "")]
		public override OperateResult<bool[]> ReadBool(string address, ushort length)
		{
			byte b = (byte)HslHelper.ExtractParameter(ref address, "s", (int)this.station);
			OperateResult<byte[]> operateResult = MelsecFxLinksOverTcp.BuildReadCommand(b, address, length, true, this.sumCheck, this.watiingTime);
			bool flag = !operateResult.IsSuccess;
			OperateResult<bool[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<bool[]>(operateResult);
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadBase(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = OperateResult.CreateFailedResult<bool[]>(operateResult2);
				}
				else
				{
					bool flag3 = operateResult2.Content[0] != 2;
					if (flag3)
					{
						result = new OperateResult<bool[]>((int)operateResult2.Content[0], "Read Faild:" + SoftBasic.ByteToHexString(operateResult2.Content, ' '));
					}
					else
					{
						byte[] array = new byte[(int)length];
						Array.Copy(operateResult2.Content, 5, array, 0, (int)length);
						result = OperateResult.CreateSuccessResult<bool[]>(Enumerable.ToArray<bool>(Enumerable.Select<byte, bool>(array, (byte m) => m == 49)));
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecFxLinksOverTcp.Write(System.String,System.Boolean[])" />
		// Token: 0x06000884 RID: 2180 RVA: 0x0002E084 File Offset: 0x0002C284
		[HslMqttApi("WriteBoolArray", "")]
		public override OperateResult Write(string address, bool[] value)
		{
			byte b = (byte)HslHelper.ExtractParameter(ref address, "s", (int)this.station);
			OperateResult<byte[]> operateResult = MelsecFxLinksOverTcp.BuildWriteBoolCommand(b, address, value, this.sumCheck, this.watiingTime);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadBase(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					bool flag3 = operateResult2.Content[0] != 6;
					if (flag3)
					{
						result = new OperateResult((int)operateResult2.Content[0], "Write Faild:" + SoftBasic.ByteToHexString(operateResult2.Content, ' '));
					}
					else
					{
						result = OperateResult.CreateSuccessResult();
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecFxLinksOverTcp.StartPLC(System.String)" />
		// Token: 0x06000885 RID: 2181 RVA: 0x0002E138 File Offset: 0x0002C338
		[HslMqttApi(Description = "Start the PLC operation, you can carry additional parameter information and specify the station number. Example: s=2; Note: The semicolon is required.")]
		public OperateResult StartPLC(string parameter = "")
		{
			byte b = (byte)HslHelper.ExtractParameter(ref parameter, "s", (int)this.station);
			OperateResult<byte[]> operateResult = MelsecFxLinksOverTcp.BuildStart(b, this.sumCheck, this.watiingTime);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadBase(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					bool flag3 = operateResult2.Content[0] != 6;
					if (flag3)
					{
						result = new OperateResult((int)operateResult2.Content[0], "Start Faild:" + SoftBasic.ByteToHexString(operateResult2.Content, ' '));
					}
					else
					{
						result = OperateResult.CreateSuccessResult();
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecFxLinksOverTcp.StopPLC(System.String)" />
		// Token: 0x06000886 RID: 2182 RVA: 0x0002E1E8 File Offset: 0x0002C3E8
		[HslMqttApi(Description = "Stop PLC operation, you can carry additional parameter information and specify the station number. Example: s=2; Note: The semicolon is required.")]
		public OperateResult StopPLC(string parameter = "")
		{
			byte b = (byte)HslHelper.ExtractParameter(ref parameter, "s", (int)this.station);
			OperateResult<byte[]> operateResult = MelsecFxLinksOverTcp.BuildStop(b, this.sumCheck, this.watiingTime);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadBase(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					bool flag3 = operateResult2.Content[0] != 6;
					if (flag3)
					{
						result = new OperateResult((int)operateResult2.Content[0], "Stop Faild:" + SoftBasic.ByteToHexString(operateResult2.Content, ' '));
					}
					else
					{
						result = OperateResult.CreateSuccessResult();
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecFxLinksOverTcp.ReadPlcType(System.String)" />
		// Token: 0x06000887 RID: 2183 RVA: 0x0002E298 File Offset: 0x0002C498
		[HslMqttApi(Description = "Read the PLC model information, you can carry additional parameter information, and specify the station number. Example: s=2; Note: The semicolon is required.")]
		public OperateResult<string> ReadPlcType(string parameter = "")
		{
			byte b = (byte)HslHelper.ExtractParameter(ref parameter, "s", (int)this.station);
			OperateResult<byte[]> operateResult = MelsecFxLinksOverTcp.BuildReadPlcType(b, this.sumCheck, this.watiingTime);
			bool flag = !operateResult.IsSuccess;
			OperateResult<string> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<string>(operateResult);
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadBase(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = OperateResult.CreateFailedResult<string>(operateResult2);
				}
				else
				{
					bool flag3 = operateResult2.Content[0] != 6;
					if (flag3)
					{
						result = new OperateResult<string>((int)operateResult2.Content[0], "ReadPlcType Faild:" + SoftBasic.ByteToHexString(operateResult2.Content, ' '));
					}
					else
					{
						result = MelsecFxLinksOverTcp.GetPlcTypeFromCode(Encoding.ASCII.GetString(operateResult2.Content, 5, 2));
					}
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x06000888 RID: 2184 RVA: 0x0002E363 File Offset: 0x0002C563
		public override string ToString()
		{
			return string.Format("MelsecFxLinks[{0}:{1}]", base.PortName, base.BaudRate);
		}

		// Token: 0x0400021F RID: 543
		private byte station = 0;

		// Token: 0x04000220 RID: 544
		private byte watiingTime = 0;

		// Token: 0x04000221 RID: 545
		private bool sumCheck = true;
	}
}
